<?php

class Arrays
{
	public static function rand(array $array, $count = 1)
	{
		$arrayCount	= count($array);
		
		if ($count < 1 || $arrayCount < 1)
			return array();
		
		if ($count > $arrayCount)
			$count = $arrayCount;
		
		$keys		= array_rand($array, $count);
		$randArray	= array();
		
		if (!is_array($keys))
			$keys = array($keys);
		
		foreach ($keys as $key)
			$randArray[$key] = $array[$key];
		
		return $randArray;
	}
	
	public static function merge(array $a1, array $a2, $recursive = false, $reserveKeys = false)
	{
		if ($reserveKeys)
		{
			foreach ($a1 as $k => $v)
			{
				if (isset($a2[$k]))
				{
					$v2 = $a2[$k];
					
					if ($recursive && is_array($v) && is_array($v2))
						$a1[$k] = self::merge($v, $v2, true, true);
					else
						$a1[$k] = $v2;
					
					unset($a2[$k]);
				}
			}
			
			foreach ($a2 as $k => $v)
				$a1[$k] = $v;
			
			return $a1;
		}
		else return $recursive ? array_merge_recursive($a1, $a2) : array_merge($a1, $a2);
	}
}
